package cn.ekuma.data.ui.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.FrameLayout;
import android.widget.SearchView;

import cn.ekuma.android.ui.filter.BaseFilterPanel;

public abstract class AbstractViewActivate extends Activity implements SearchView.OnQueryTextListener,BaseFilterPanel.FilterPanelAdapter {

	LinearLayout titlePanel;
	FrameLayout contentLayout;
	private ImageButton zoomImgBtn;
    protected BaseFilterPanel filterPanel;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.abstract_view);

		titlePanel=(LinearLayout)findViewById(R.id.titlePanel);

        filterPanel=new BaseFilterPanel(this.getBaseContext(),this);

        titlePanel.addView(filterPanel);
        contentLayout = (FrameLayout) findViewById(R.id.contentPanel);

		View c = createContentPanel();
		if (c != null){
            FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            contentLayout.addView(c,lp1);
		    zoomImgBtn=new ImageButton(this);
			zoomImgBtn.setImageResource(R.drawable.tree_collapse);
			zoomImgBtn.setOnClickListener(listener);
			
			FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
			//此处相当于布局文件中的Android:layout_gravity属性   
			lp.gravity = Gravity.BOTTOM|Gravity.RIGHT;  
			contentLayout.addView(zoomImgBtn,lp);
		}
	}

	public abstract void filterParaReset() ;

	public abstract void initAdvSeachView();

	public abstract boolean isQuickSeachAble();

	public abstract void onQuickSeach(String inputText);

	public abstract View createFilterPanel();

	public abstract View createAdvFilterPanel();

	public abstract View createContentPanel();
	
	private boolean waitDouble = true;

	private static final int DOUBLE_CLICK_TIME = 350;
	
	 OnClickListener listener = new OnClickListener(){
		      public void onClick(View v) {
		         if(waitDouble == true){
		            waitDouble = false;
		            Thread thread = new Thread(){
		               @Override
		               public void run(){
		                  try {
		                     sleep(DOUBLE_CLICK_TIME);
		                     if(waitDouble == false){
		                        waitDouble = true;
		                        singleClick();
		                     }
		                  } catch (InterruptedException e) {
		                     e.printStackTrace();
		                  }
		               }
		            };
		            thread.start();
		         }else{
		            waitDouble = true;
		            doubleClick();
		         } 
		      }
		   };
		   
	private void singleClick(){
	 }
		  
	private void doubleClick(){
		titlePanel.setVisibility(titlePanel.getVisibility()==View.GONE?View.VISIBLE:View.GONE);
		zoomImgBtn.setImageResource(titlePanel.getVisibility()==View.GONE?R.drawable.tree_expand:R.drawable.tree_collapse);
    }

    /**添加ActionBar的Menu*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(isQuickSeachAble()) {
            getMenuInflater().inflate(R.menu.menu_find, menu);
            SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
            searchView.setOnQueryTextListener(this);
        }
        return super.onCreateOptionsMenu(menu);
    }

    /**监听子菜单的点击事件*/
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        onQuickSeach(query);
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return true;
    }
}
